package ubadb.core.util.collections;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import ubadb.core.util.Predicate;

/**
 * Utility class for handling collections.
 */
public class CollectionUtils
{
	
	/**
	 * Private default constructor to prevent instantiation.
	 */
	private CollectionUtils()
	{
		// nop
	}
	
	/**
	 * Filters a collection, returning another one that contains only the elements
	 * for which the condition evaluates to true.
	 * 
	 * @param input any collection
	 * @param condition condition that determines which elements should be in the result
	 * @return a new collection with the elements that meet the condition
	 */
	public static <T> Collection<T> filter(Collection<T> input, Predicate<T> condition)
	{
		List<T> result = new LinkedList<>();
		
		for(T element : input)
			if(condition.evaluate(element))
				result.add(element);
		
		return result;
	}
}
